package com.xuecheng.comment.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.comment.model.dto.CommentReplyDTO;
import com.xuecheng.comment.common.constant.CommentErrorCode;
import com.xuecheng.comment.convert.CommentReplyConvert;
import com.xuecheng.comment.entity.Comment;
import com.xuecheng.comment.entity.CommentReply;
import com.xuecheng.comment.mapper.CommentMapper;
import com.xuecheng.comment.mapper.CommentReplyMapper;
import com.xuecheng.comment.service.CommentReplyService;
import com.xuecheng.comment.service.CommentService;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.exception.ExceptionCast;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

/**
 * 评论回复 服务实现类
 *
 * @author itcast
 * @since 2019-10-31
 */
@Slf4j
@Service
public class CommentReplyServiceImpl extends ServiceImpl<CommentReplyMapper, CommentReply> implements CommentReplyService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CommentService commentService;

    /**
     * 1.判断关键数据
     * commentId
     * 2.根据commentId 获得comment数据
     * 判断评论数据是否存在
     * 判断评论是否禁止回复
     * 3.创建评论回复数据
     * 4.po转dto封装数据并返回
     */
    @Transactional
    public RestResponse<CommentReplyDTO> commentReply(CommentReplyDTO replyDTO) {

        //1.判断关键数据
        if (ObjectUtils.isEmpty(replyDTO.getCommentId())) {
            return RestResponse.validfail(CommonErrorCode.E_100101);
        }
        //2.根据commentId 获得comment数据
        Comment comment = commentService.getById(replyDTO.getCommentId());
        // 判断评论数据是否存在
        if (ObjectUtils.isEmpty(comment)) {
            return RestResponse.validfail(CommentErrorCode.E_180101);
        }
        // 判断评论是否禁止回复
        if (CommonEnum.USING_FLAG.getCodeInt() == comment.getForbidReply()) {
            return RestResponse.validfail(CommentErrorCode.E_180100);
        }

        //3.创建评论回复数据
        //先查询是否已经存在评论回复
        LambdaQueryWrapper<CommentReply> replyQueryWrapper = new LambdaQueryWrapper<>();
        replyQueryWrapper.eq(CommentReply::getCommentId, replyDTO.getCommentId());
        replyQueryWrapper.eq(CommentReply::getParentId, CommentReply.PARENT_ID_IS);

        CommentReply commentReply = this.getOne(replyQueryWrapper);
        if (ObjectUtils.isEmpty(commentReply)) {
            //无评论回复  创建  parentId为0
            commentReply = CommentReplyConvert.INSTANCE.dto2entity(replyDTO);

            commentReply.setId(null);
            commentReply.setParentId(CommentReply.PARENT_ID_IS);
            commentReply.setStatus(CommonEnum.USING_FLAG.getCodeInt());
            commentReply.setUserName(comment.getUserName());
            commentReply.setReplyDate(LocalDateTime.now());

            boolean save = this.save(commentReply);
            if (!save) {
                return RestResponse.validfail(CommentErrorCode.E_180001);
            }
        } else {
            //有评论回复 新的评论 parentId为1
            commentReply = null;
            commentReply = CommentReplyConvert.INSTANCE.dto2entity(replyDTO);

            commentReply.setId(null);
            commentReply.setParentId(CommentReply.PARENT_ID_NOT);
            commentReply.setStatus(CommonEnum.USING_FLAG.getCodeInt());
            commentReply.setUserName(comment.getUserName());
            commentReply.setReplyDate(LocalDateTime.now());

            boolean save = this.save(commentReply);
            if (!save) {
                return RestResponse.validfail(CommentErrorCode.E_180001);
            }
        }

        //4.po转dto封装数据并返回
        CommentReplyDTO commentReplyDTO = CommentReplyConvert.INSTANCE.entity2dto(commentReply);

        return RestResponse.success(commentReplyDTO);
    }

//    @Transactional
//    public CommentReplyDTO commentReply(CommentReplyDTO replyDTO) {
//
//        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper();
//
//        queryWrapper.eq(Comment::getId, replyDTO.getCommentId());
//
//        Comment comment = commentMapper.selectOne(queryWrapper);
//
//        if (comment.getForbidReply() == CommonEnum.USING_FLAG.getCodeInt()) {
//            ExceptionCast.cast(CommentErrorCode.E_180100);
//        }
//
//        comment.setReplyStatus(CommonEnum.USING_FLAG.getCodeInt());
//
//        commentMapper.updateById(comment);
//
//        // TODO CommonEnum.USING_FLAG.getCodeInt()
//        CommentReply commentReply = CommentReplyConvert.INSTANCE.dto2entity(replyDTO);
//
//        commentReply.setStatus(CommonEnum.USING_FLAG.getCodeInt());
//
//        commentReply.setReplyDate(LocalDateTime.now());
//
//        this.save(commentReply);
//
//        return CommentReplyConvert.INSTANCE.entity2dto(commentReply);
//
//    }


}
